/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



var validationApp = angular.module('validationApp', []);

validationApp.controller('UserController',function($scope,$http) {

    $scope.formData = {};
    
    $scope.processForm = function () {
        dataLoading();

        $http({
            method: 'POST',
            url: 'RegisterUser.do',
            data: $.param($scope.formData),
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function () {
            dataUnLoading();

            $(".msg").delay(2000).html('<p class="alert alert-success" role="alert"><strong>Cadastro realizado com sucesso!</strong></p>').fadeIn('slow');
            window.setTimeout(function () {
                $(location).attr('href', "Home.do");
            }, 3000);
        }).error(function () {
            dataUnLoading();
            $(".msg").html('<p class="alert alert-danger"><strong>Erro no sistema!</strong></p>').fadeIn('fast'),
                    setTimeout(function () {
                        $(".msg").hide();
                    }, 1000);
        });
    };

    var userData = angular.copy($scope.formData);

    $scope.resetForm = function () {
        $scope.formData = angular.copy(userData);
        $scope.formData.$invalid();
    };

    $scope.doAjaxPostValidaEmail= function () {
         dataLoading();
        $http({
            method: 'POST',
            url: 'BuscaUser.do',
            data: $.param($scope.formData),
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}

        }).success(function () {
            dataUnLoading();
                $(".msg").html('<p class="alert alert-warning">Email já Cadastrado!</p>').fadeIn('fast'),
                        setTimeout(function () {
                            $(".msg").hide();
                        }, 1000);
                
                $scope.formData = angular.copy(userData);
                $scope.formData.txtEmail.$invalid();
                
                
                return true;
                        
        }).error(function (){
            dataUnLoading();
            console.log($scope.formData.txtEmail);
            return false;
        });
        

    };

});